package com.bwie.config;

import com.bwie.websocket.DriverWebSocketHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket // 启用WebSocket支持
public class WebSocketConfig implements WebSocketConfigurer {

    private final DriverWebSocketHandler webSocketHandler;

    // 注入自定义的消息处理器
    public WebSocketConfig(DriverWebSocketHandler webSocketHandler) {
        this.webSocketHandler = webSocketHandler;
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 注册WebSocket端点，允许前端通过ws://localhost:8080/ws连接
        // ws://localhost:9011/ws
        // ws://localhost:9011/driver
        registry.addHandler(webSocketHandler, "/ws")
                .setAllowedOrigins("*"); // 允许跨域（生产环境需指定具体域名）
    }
}

